package chapter6;

/**
 * toString是Object类的方法，所有自定义对象可以重写该方法
 * 该方法用于自我描述，打印对象时就会输出toString方法中的内容
 */
public class ToStringTest {
    public static void main(String[] args) {
        Apple apple = new Apple("绿色", 50.0);
        System.out.println(apple);
    }
}

class Apple {
    private String color;
    private double weight;

    public Apple() {
    }

    public Apple(String color, double weight) {
        this.color = color;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "一个苹果，颜色是:" + color + ", 重量是:" + weight + "g";
    }
}
